package com.sf.fy.uav.applet.order.controller;

import com.sf.fy.uav.applet.order.controller.service.OrderService;
import com.sf.fy.uav.applet.user.service.AboutUserService;
import com.sf.fy.uav.common.request.order.task.ConfirmPayRequest;
import com.sf.fy.uav.common.utils.BeanUtil;
import com.sf.fy.uav.common.utils.Rs;
import com.sf.fy.uav.order.controller.FeedbackTypeRest;
import com.sf.fy.uav.order.controller.OrderInfoRest;
import com.sf.fy.uav.order.service.IOrderInfoService;
import com.sf.fy.uav.user.rest.UserRest;
import com.sfpay.sdk.core.callback.CallbackHelper;
import com.sfpay.sdk.entity.callback.TradePayCallback;
import com.sfpay.sdk.entity.callback.TradeRefundCallback;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.security.SecureRandom;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author LTQ
 */
@RestController
@RequestMapping("/home/order/")
public class OrderPublicController {
    private final static Logger logger = LoggerFactory.getLogger(OrderPublicController.class);

    @Autowired
    private UserRest userRest;

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderInfoRest orderInfoRest;

    @Autowired
    AboutUserService aboutUserService;

    @Autowired
    private IOrderInfoService iOrderInfoService;

    @Autowired
    private FeedbackTypeRest feedbackTypeRest;

    @PostMapping("refund-confirm")
    public String refundConfirmPay(HttpServletRequest httpServletRequest) {
        Enumeration<String> names = httpServletRequest.getParameterNames();
        Map<String, String> params = new HashMap<>();
        while (names.hasMoreElements()) {
            String k = names.nextElement();
            String v = httpServletRequest.getParameter(k);
            if (v == null || v.isEmpty()) {
                continue;
            }
            try {
                params.put(k, URLDecoder.decode(v, "utf-8"));
            } catch (UnsupportedEncodingException ignored) {
            }
        }

        logger.info("转换前参数是：" + params);
        TradeRefundCallback tradeRefundCallback = CallbackHelper.handle(params, TradeRefundCallback.class);
        System.out.println(tradeRefundCallback);
        logger.info("参数是：" + tradeRefundCallback.toString());
        iOrderInfoService.confirmRefund(tradeRefundCallback);
        return "success";
    }

    @PostMapping("confirm")
    public String confirmPay(HttpServletRequest httpServletRequest) {
        if (new SecureRandom().nextBoolean()) {
            System.out.println("进入另一个流程了，another flow");
        }
        logger.info("进入支付确认");
        TradePayCallback tradePayCallback;
        try {
            // 2. 自行处理参数获取及相关处理, 保证所有参数都获取出来
            Map<String, String> params = new HashMap<>();

            Enumeration<String> names = httpServletRequest.getParameterNames();
            while (names.hasMoreElements()) {
                String k = names.nextElement();
                String v = httpServletRequest.getParameter(k);
                if (v == null || v.isEmpty()) {
                    continue;
                }
                try {
                    params.put(k, URLDecoder.decode(v, "utf-8"));
                } catch (UnsupportedEncodingException ignored) {
                }
            }

            logger.info("转换前参数是：" + params);
            tradePayCallback = CallbackHelper.handle(params, TradePayCallback.class);
            logger.info("参数是：" + tradePayCallback.toString());

            ConfirmPayRequest confirmPayRequest = BeanUtil.copyProperties(tradePayCallback, ConfirmPayRequest.class);
            orderService.confirmPay(confirmPayRequest);
        } catch (Exception e) {
            e.printStackTrace();
            // 回调参数处理失败, 返回失败(非"success") 等待重试
        }

        return "success";
    }

}
